package bfsalgorithm;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 1765. 地图中的最高点
 */
public class Exercise10 {
    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    public int[][] highestPeak(int[][] isWater) {
        int m = isWater.length,n = isWater[0].length;
        boolean[][] visit = new boolean[m][n];

        int[][] height = new int[m][n];

        Queue<int[]> q = new LinkedList<>();
        for(int i = 0;i < m;i++) {
            for(int j = 0;j < n;j++) {
                if(isWater[i][j] == 1) {
                    q.add(new int[]{i,j});
                    visit[i][j] = true;
                }
            }
        }

        while(!q.isEmpty()) {
            int[] tmp = q.poll();
            int a = tmp[0],b = tmp[1];
            for(int k = 0;k < 4;k++) {
                int x = a + dx[k];
                int y = b + dy[k];
                if(x >= 0 && x < m && y >= 0 && y < n && !visit[x][y]) {
                    height[x][y] = height[a][b] + 1;
                    q.add(new int[]{x,y});
                    visit[x][y] = true;
                }
            }
        }

        return height;
    }
}
